/**
 * 
 */
package org.dc.agent.xml;


import org.apache.commons.digester3.Digester;
import org.apache.log4j.Logger;
import org.dc.agent.beans.Oid;
import org.dc.agent.beans.Policy;
import org.dc.agent.beans.Policys;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author 徐春鹏 日期 2012-11-9
 */
public class XmlOper {

	private static Logger logger = Logger.getLogger(XmlOper.class);

	private Digester digester;

	public XmlOper() {

		digester = new Digester();
		logger.warn("--digester--classloader--" + digester.getClassLoader());
		logger.warn("--XmlParser--classloader--"
				+ this.getClass().getClassLoader().getParent());
		digester.addObjectCreate("policys", Policys.class);
		digester.addSetProperties("policys");

		digester.addObjectCreate("policys/policy", Policy.class.getName());
		digester.addSetProperties("policys/policy");
		digester.addSetNext("policys/policy", "addPolicy",
				Policy.class.getName());

		digester.addObjectCreate("policys/policy/oid", Oid.class.getName());
		digester.addSetProperties("policys/policy/oid");
		digester.addSetNext("policys/policy/oid", "addOid", Oid.class.getName());

	}

	public Policys getPolicy() {

		String property = System.getProperty("user.dir");
		File f = new File(property + "\\policy.xml");
		try {
			FileInputStream fileInputStream = new FileInputStream(f);
			Policys policy = digester.parse(fileInputStream);
			return policy;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}
		return null;
	}

}
